package com.symbio.ishenapi.order.ExcelExport;

import com.symbio.ishenapi.order.model.TmServiceOrder;
import org.apache.poi.hssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

public class ExcelExportDoctorUtil {

    public String ExportNoResponse(String sheetName, String titleName,
                                   String fileName, int columnNumber, int[] columnWidth,
                                   String[] columnName, List<TmServiceOrder> dataList, HttpServletResponse response) throws Exception {
        if (columnNumber == columnWidth.length && columnWidth.length == columnName.length) {
            // 第一步，创建一个webbook，对应一个Excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet(sheetName);
            // sheet.setDefaultColumnWidth(15); //统一设置列宽
            for (int i = 0; i < columnNumber; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    if (i == j)
                    {
                        sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽
                    }
                }
            }
            // 创建第1行 也就是表头
            HSSFRow row = sheet.createRow((int) 0);
            row.setHeightInPoints(37);// 设置表头高度

            // 第四步，创建表头单元格样式 以及表头的字体样式
            HSSFCellStyle style = wb.createCellStyle();
            style.setWrapText(true);// 设置自动换行
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

            HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
            headerFont.setFontName("黑体"); // 设置字体类型
            headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
            style.setFont(headerFont); // 为标题样式设置字体样式

            // 第四.一步，创建表头的列
            for (int i = 0; i < columnNumber; i++)
            {
                HSSFCell cell = row.createCell(i);
                cell.setCellValue(columnName[i]);
                cell.setCellStyle(style);
            }

            // 第五步，创建单元格，并设置值
            for (int i = 0; i < dataList.size(); i++)
            {
                row = sheet.createRow((int) i + 1);
                    for (int j = 0; j < columnNumber; j++)
                    {
                        row.createCell(0).setCellValue(dataList.get(i).getDoctorTEL());
                        row.createCell(1).setCellValue(dataList.get(i).getDoctorName());
                        row.createCell(2).setCellValue(dataList.get(i).getHospitalName());
                        row.createCell(3).setCellValue(dataList.get(i).getPostName());
                        if(dataList.get(i).getCreateTime() == null){
                            row.createCell(4).setCellValue("");
                        }else{
                            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                            String dateString = formatter.format(dataList.get(i).getCreateTime());
                            row.createCell(4).setCellValue(dateString);
                        }
                        row.createCell(5).setCellValue(dataList.get(i).getServiceFee());
                        row.createCell(6).setCellValue(dataList.get(i).getMedicineFee());
                        row.createCell(7).setCellValue(dataList.get(i).getTotal());
                    }
            }

            String str = "";
            // 第六步，将文件存到浏览器设置的下载位置
            String filename = fileName + ".xls";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));
            OutputStream out = response.getOutputStream();
            try {
                wb.write(out);// 将数据写出去
                str = "导出" + fileName + "成功！";
                System.out.println(str);
                //out.close();
            } catch (Exception e) {
                e.printStackTrace();
                String str1 = "导出" + fileName + "失败！";
                System.out.println(str1);
            } finally {
                out.close();
                System.out.println("8888888888888");
                return str;
            }
        } else {
            System.out.println("列数目长度名称三个数组长度要一致");
            return "列数目长度名称三个数组长度要一致";
        }
    }
}
